<?php
/**
 * Created by PhpStorm.
 * User: 86151
 * Date: 2023/10/11
 * Time: 11:12
 */

namespace app\api\controller;
use app\common\controller\Api;
use think\Config;
use think\Db;
use app\admin\model\Auth;
use app\admin\model\Index;
use app\admin\model\Index as IndexModel;

class Member extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    /**
     * 权益分类
     */
    public function classlist(){
        $vip_id = input('vip_id');
        $zonefl_id = input('zonefl_id');
        $data = [];
        if($vip_id){
            $data = Db::name('operator_zonefl')->where('vip_id',$vip_id)->where('switch',1)->order('weigh ASC')->field('id,name')->select();
        }
        if($zonefl_id){
            $data = Db::name('operator_class')->where('zonefl_id',$zonefl_id)->where('switch',1)->order('weigh ASC')->field('id,cl_name')->select();
        }
        $this->success("成功",$data);
    }
    /**
     * 兑换权益 商品列表
     */
    public function goodsList(){
        $zonefl_id= input('zonefl_id');
        $class_id = input('class_id');
        $page = input('page', 1);
        $showpage = input('showpage', 18);
        $pageall = ($page - 1) * $showpage;
        $filed = input('filed','order');
        $px_type= input('px_type','ASC');
        $city = input('city');
        $content = input('content');
        $map=[];
        if($zonefl_id){
            $map['zonefl_id'] = $zonefl_id;
        }
        if($class_id){
            $map['cla_id'] = $class_id;
        }else{
            $map['cla_id'] =['in',Db::name('operator_class')->where('switch',1)->where('zonefl_id',$zonefl_id)->column('id')];
        }
        if($city){
            $city_id = Db::name("ali_city")->where(['city_name'=>$city])->value('city_code');
            $map['city'] = ['like',"%$city_id%"];
        }
        if($content){
            $map['resource_names'] = ['like',"%$content%"];
        }
        $operator_exchange_goods = Db::name('operator_exchange_goods')
            ->limit($pageall, $showpage)
            ->where('xy',0)
            ->where($map)
            ->order($filed,$px_type)
            ->select();
        $data = index::getAnyTypeData($operator_exchange_goods);
        $this->success("ok",$data);
    }

    /**
     * 原子权益
     */
    public function quanyi(){
        $user_id = $this->auth->id;
        $userInfo = Auth::userInfo($user_id);
        $user_qy = Db::name('user_qy')->group('vip_id')->where('vip_id','in',[123,124,139])->where('mobile',$userInfo['mobile'])
            ->whereTime('end_time','>',date('Y-m-d H:i:s'))->select();
        foreach ($user_qy as &$v){
            $operator_vip = Db::name('operator_vip')->where('id',$v['vip_id'])->find();
            $v['image'] = $operator_vip['quanyi_image'];
        }
        $this->success("信息",$user_qy);
    }

    public function rules(){
        $vip_id = input('vip_id');
        $rules = Db::name('operator_vip')->where('id',$vip_id)->value('rules');
        $this->success("获取成功",$rules);
    }

    /**
     * 我的会员信息
     * @param string $token token
     */
    public function myVip(){
        $user_id = $this->auth->id;
        $userInfo =Auth::userInfo($user_id);
        if($userInfo['vip_id']){
            $is_vip = 1;
            $infos ='会员到期:'.date('Y-m-d',strtotime($userInfo['vip_end_time']));
        }else{
            $is_vip = 0;
            $vip_list = Db::name('user_vip_list')->where('mobile',$userInfo['mobile'])->where('deal',1)->find();
            if($vip_list){
                $infos = '会员申请中';
            }else{
                $infos = '你的会员已到期';
            }
        }
        $operator_vip = Db::name('operator_vip')->where('id',$userInfo['vip_id'])->find();
        if(!$operator_vip){
            $operator = Db::name('operator')->where('name',$userInfo['operator'])->field('id')->find();
            if($operator){
                $operator_vip = Db::name('operator_vip')->where('operator_id',$operator['id'])->where('is_do',1)->find();
                $userInfo['vip_id'] = $operator_vip['id'];
            }else{
                $operator_vip = Db::name('operator_vip')->where('operator_id',1)->where('is_do',1)->find();
                $userInfo['vip_id'] = $operator_vip['id'];
            }
        }
        $userInfo['infos'] = $infos;
        $userInfo['is_vip'] = $is_vip;
        $userInfo['vip_name'] =$operator_vip['vip_name'];
        $userInfo['rules'] =$operator_vip['rules'];
        $this->success("获取成功",$userInfo);
    }

}